package com.cmf.myproject.service.project;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cmf.myproject.dal.mybatis.mapper.ProjectMapper;
import com.cmf.myproject.dal.mybatis.model.Project;
import com.cmf.myproject.dal.mybatis.model.ProjectExample;
import com.cmf.myproject.dal.mybatis.model.ProjectExample.Criteria;
import com.cmf.myproject.service.project.dto.ProjectAddItemReqDto;
import com.cmf.myproject.service.project.dto.ProjectQueryFilter;
import com.cmf.myproject.service.project.dto.ProjectUpdateItemReqDto;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.system.core.results.ResultBase;
import com.system.core.view.layui.table.TableViewRspDto;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class ProjectService {
	
	private SimpleDateFormat timeformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	@Autowired
	private ProjectMapper projectMapper;
	
	public List<Project> projectAllEnableList(){
		ProjectExample projectExample = new ProjectExample();
		projectExample.createCriteria().andIsEnableEqualTo(true);
		return projectMapper.selectByExample(projectExample);
	}
	
	/**
	 * 页面列表查询
	 * @return TableViewRspDto 返回TableViewRspDto视图对象，符合Layui规范
	 */
	public TableViewRspDto<Project> projectList(ProjectQueryFilter filter){
		log.debug("[project]查询入参：{}",filter);
		TableViewRspDto<Project> pageResult = new TableViewRspDto<Project>();
		ProjectExample example = new ProjectExample();
		Criteria criteria = example.createCriteria();
		//在此插入查询条件start
		if(filter.getId() != null) {
			criteria.andIdEqualTo(filter.getId());
		}
		if(StringUtils.isNotBlank(filter.getName())) {
			criteria.andNameLike("%"+filter.getName()+"%");
		}
		if(filter.getOwnerType() != null) {
			criteria.andOwnerTypeEqualTo(filter.getOwnerType());
		}
		/**
		 * jdbcType:timestamp
		 */
		try {
			if(StringUtils.isNotBlank(filter.getStartTimeStrStart())) {
				criteria.andStartTimeGreaterThanOrEqualTo(timeformat.parse(filter.getStartTimeStrStart()));
			}
			if(StringUtils.isNotBlank(filter.getStartTimeStrEnd())) {
				criteria.andStartTimeLessThanOrEqualTo(timeformat.parse(filter.getStartTimeStrEnd()));
			}
		} catch (ParseException e) {
			e.printStackTrace();
			pageResult.setMessage(e.getMessage());
			return pageResult;
		}
		if(filter.getIsEnable() != null) {
			criteria.andIsEnableEqualTo(filter.getIsEnable());
		}
		/**
		 * jdbcType:timestamp
		 */
		try {
			if(StringUtils.isNotBlank(filter.getCreateTimeStrStart())) {
				criteria.andCreateTimeGreaterThanOrEqualTo(timeformat.parse(filter.getCreateTimeStrStart()));
			}
			if(StringUtils.isNotBlank(filter.getCreateTimeStrEnd())) {
				criteria.andCreateTimeLessThanOrEqualTo(timeformat.parse(filter.getCreateTimeStrEnd()));
			}
		} catch (ParseException e) {
			e.printStackTrace();
			pageResult.setMessage(e.getMessage());
			return pageResult;
		}
		/**
		 * jdbcType:timestamp
		 */
		try {
			if(StringUtils.isNotBlank(filter.getUpdateTimeStrStart())) {
				criteria.andUpdateTimeGreaterThanOrEqualTo(timeformat.parse(filter.getUpdateTimeStrStart()));
			}
			if(StringUtils.isNotBlank(filter.getUpdateTimeStrEnd())) {
				criteria.andUpdateTimeLessThanOrEqualTo(timeformat.parse(filter.getUpdateTimeStrEnd()));
			}
		} catch (ParseException e) {
			e.printStackTrace();
			pageResult.setMessage(e.getMessage());
			return pageResult;
		}
		//在此插入查询条件end
		
		Page<Project> page = PageHelper.startPage(filter.getPage(), filter.getLimit());
		example.setOrderByClause("create_time desc");
		pageResult.setData(projectMapper.selectByExample(example));
		pageResult.setCount(page.getTotal());
		pageResult.setCode(0);
		pageResult.setMessage("查询成功");
		return pageResult;
	}
	
	/**
	 * 根据主键获取单条数据
	 * @param project 使用project对象的主键属性Id类型是Long
	 */
	public Project projectGetByPrimaryKey(Project project) {
		Project result = projectMapper.selectByPrimaryKey(project.getId());
		return result;
	}
	
	/**
	 * 根据主键更新数据
	 * @param project 被更新对象（请注意，只更新有值属性）
	 */
	public ResultBase projectUpdateByPrimaryKey(ProjectUpdateItemReqDto projectUpdateItemReqDto) {
		ResultBase resultBase = new ResultBase();
		Project project = new Project();
		BeanUtils.copyProperties(projectUpdateItemReqDto, project);
		projectMapper.updateByPrimaryKeySelective(project);
		resultBase.setSuccess(true);
		resultBase.setMessage("更新成功");
		return resultBase;
	}
	
	/**
	 * 根据主键删除数据
	 * @param keys 被删除对象主键List集合
	 */
	@Transactional
	public ResultBase projectDeleteByPrimaryKey(List<Long> keys) {
		ResultBase resultBase = new ResultBase();
		try {
			keys.forEach((key)->{
				projectMapper.deleteByPrimaryKey(key);
			});
		} catch (Exception e) {
			resultBase.setMessage(e.getMessage());
			return resultBase;
		}
		resultBase.setSuccess(true);
		resultBase.setMessage("成功删除"+keys.size()+"条记录");
		return resultBase;
	}
	
	/**
	 * 新增数据
	 * @param projectAddItemReqDto 新增对象
	 */
	@Transactional
	public ResultBase projectSave(ProjectAddItemReqDto projectAddItemReqDto) {
		ResultBase resultBase = new ResultBase();
		Project project = new Project();
		BeanUtils.copyProperties(projectAddItemReqDto, project);
		projectMapper.insertSelective(project);
		resultBase.setSuccess(true);
		resultBase.setMessage("新增成功");
		return resultBase;
	}
}
